package javaAdvanced;

/**
 * @author cuiH
 * Date: 13-10-29
 */
public class MultiThreadShareData {
    /*
    * 多线程共享数据
    * */
    public static void main(String[] args) {
          final ShareData1 data1 = new ShareData1();
          new   Thread(new Runnable() {
              @Override
              public void run() {
                  data1.decrement();
              }
          }).start();
          new   Thread(new Runnable() {
              @Override
              public void run() {
                   data1.increment();
              }
          }).start();
    }

    //继承run方法
    /**
     * 如果多个线程共享一个代码，可以在runnable里面继承即可，最简单的
     * 另外，将共享数据放在
     */
    static class ShareData1 implements Runnable{
        private int countPiao = 100;
        private int j = 0;
        public synchronized void increment(){
            j++;
        }

        public synchronized void decrement(){
            j++;
        }

        @Override
        public void run() {
        }
    }
}
